#pragma once

#ifdef ENABLE_FREERTOS
#include "cmsis_os2.h"
#endif
#include <cstdint>

import key;

namespace utils::key
{
    class Control
    {
    public:
        static void handler();
    };
}


#ifdef ENABLE_FREERTOS

    /**
     * @brief FreeRTOS按键适配器
     */
    template<>
    struct utils::KeyAdapter<struct FreeRTOS>
    {
    static inline void handler(){key::Control::handler();}

        // FreeRTOS信号量操作
        static inline void notify_from_isr()
        {
            osSemaphoreRelease(sem_);
        }

        static inline bool acquire()
        {
            return osSemaphoreAcquire(sem_, osWaitForever);
        }


        static inline void init()
        {
            sem_ = osSemaphoreNew(1, 1, nullptr);
        }

        static inline osSemaphoreId_t sem_ = nullptr;
        static inline osThreadId_t thread_id_ = nullptr;
    };

#else
    /**
     * @brief 裸机按键适配器
     */
    template<>
    struct utils::KeyAdapter<struct Bare>
    {
        static inline void notify_from_isr()
        {
            sign = 0xFF;
        }

        static inline bool acquire()
        {
            if (sign)
            {
                sign = 0;
                return true;
            }
            return false;
        }

        static inline void init() {}

        static inline void handler(){key::Control::handler();}

        static uint8_t inline sign = 0;
    };
#endif



#ifdef ENABLE_FREERTOS
// 使用别名简化
using PlatformKey = Key::Core<KeyAdapter<FreeRTOS>>;// RTOS下的按键
#else
    using PlatformKey = utils::Key::Core<utils::KeyAdapter<Bare> >; // 裸机下的按键
#endif


